﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ClientSide.Common;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Collections;
using System.ComponentModel;

namespace Container
{

    /// <summary>
    /// DeploymentCatalog bileşenlerini yazmak için partial yazıldı bu kısım
    /// </summary>
    public partial class ContainerPage
    {

        [Import]
        public IDeploymentCatalogService CatalogService { get; set; }

        // Specifies that a property, field, or parameter should be populated with all
        // matching exports by the System.ComponentModel.Composition.Hosting.CompositionContainer.
        [ImportMany(AllowRecomposition = true)]
        public Lazy<IPage>[] MEFModuleList { get; set; }
        //Buradaki Core.IPage container'a yüklenecek xap ların Interface'idir

        public void LoadXAP(string strSelectedXAP)
        {

            // Have MEF load the .xap
            CatalogService.AddXap(strSelectedXAP, (e) =>
            {
                // Create a name for the .xap without the ".xap" part
                string strRevisedSelectedXAPName = strSelectedXAP.Replace(".xap", "");

                // Get the selected .xap 
                var SelectedMEFModule = (from Module in MEFModuleList.ToList()
                                         where Module.Value.ToString().Contains(strRevisedSelectedXAPName)
                                         select Module).FirstOrDefault();

                // Add the .xap to the main page
                Container.Child = (UIElement)SelectedMEFModule.Value;
            }
                );

        }


        private void LoadSelectedModule(string strSelectedXAP)
        {
            // Ensure that we have a Panel to add the .xap to
            if (Container != null)
            {
                // Create a name for the .xap without the ".xap" part
                string strRevisedSelectedXAPName = strSelectedXAP.Replace(".xap", ".");

                // Determine if the .xap is already loaded
                //var SelectedMEFModuleInPanel = (from Module in MefPanel.Children.Cast<UserControl>()
                //                                where Module.ToString().Contains(strRevisedSelectedXAPName)
                //                                select Module).FirstOrDefault();

                // If the .xap is not loaded
                //if (SelectedMEFModuleInPanel == null)
                //{
                // Clear the panel
                //pageContainer1.Children.Clear();

                // Get the selected .xap 
                var SelectedMEFModule = (from Module in MEFModuleList.ToList()
                                         where Module.Value.ToString().Contains(strRevisedSelectedXAPName)
                                         select Module).FirstOrDefault();

                // Add the .xap to the main page
                Container.Child = (UIElement)SelectedMEFModule.Value;

            }
        }


    }
}
